package com.example.HaiDLaobackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.HaiDLaobackend.common.ApiResponse;
import com.example.HaiDLaobackend.entity.UserAddressEntity;
import com.example.HaiDLaobackend.service.UserAddressService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user/addresses")
public class AddressController {

    private final UserAddressService userAddressService;

    public AddressController(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    @PostMapping
    public ApiResponse<UserAddressEntity> create(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId,
                                                 @RequestBody UserAddressEntity body) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        if (body == null) return ApiResponse.error(400, "参数错误");
        if (body.getIsDefault() != null && body.getIsDefault() == 1) {
            userAddressService.update(new LambdaUpdateWrapper<UserAddressEntity>()
                    .set(UserAddressEntity::getIsDefault, 0)
                    .eq(UserAddressEntity::getUserId, currentUserId));
        }
        UserAddressEntity entity = new UserAddressEntity();
        entity.setUserId(currentUserId);
        entity.setReceiver(body.getReceiver());
        entity.setPhone(body.getPhone());
        entity.setProvince(body.getProvince());
        entity.setCity(body.getCity());
        entity.setDistrict(body.getDistrict());
        entity.setDetail(body.getDetail());
        entity.setIsDefault(body.getIsDefault() != null ? body.getIsDefault() : 0);
        userAddressService.save(entity);
        return ApiResponse.ok(entity);
    }

    @GetMapping
    public ApiResponse<List<UserAddressEntity>> list(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        List<UserAddressEntity> list = userAddressService.list(new LambdaQueryWrapper<UserAddressEntity>()
                .eq(UserAddressEntity::getUserId, currentUserId)
                .orderByDesc(UserAddressEntity::getIsDefault)
                .orderByDesc(UserAddressEntity::getId));
        return ApiResponse.ok(list);
    }

    @PutMapping("/{id}")
    public ApiResponse<UserAddressEntity> update(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId,
                                                 @PathVariable Long id,
                                                 @RequestBody UserAddressEntity body) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        UserAddressEntity origin = userAddressService.getOne(new LambdaQueryWrapper<UserAddressEntity>()
                .eq(UserAddressEntity::getId, id)
                .eq(UserAddressEntity::getUserId, currentUserId));
        if (origin == null) return ApiResponse.error(404, "地址不存在");

        origin.setReceiver(body.getReceiver());
        origin.setPhone(body.getPhone());
        origin.setProvince(body.getProvince());
        origin.setCity(body.getCity());
        origin.setDistrict(body.getDistrict());
        origin.setDetail(body.getDetail());
        userAddressService.updateById(origin);
        return ApiResponse.ok(origin);
    }

    @PostMapping("/{id}/default")
    public ApiResponse<Boolean> setDefault(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId,
                                           @PathVariable Long id) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        UserAddressEntity origin = userAddressService.getOne(new LambdaQueryWrapper<UserAddressEntity>()
                .eq(UserAddressEntity::getId, id)
                .eq(UserAddressEntity::getUserId, currentUserId));
        if (origin == null) return ApiResponse.error(404, "地址不存在");

        userAddressService.update(new LambdaUpdateWrapper<UserAddressEntity>()
                .set(UserAddressEntity::getIsDefault, 0)
                .eq(UserAddressEntity::getUserId, currentUserId));
        origin.setIsDefault(1);
        userAddressService.updateById(origin);
        return ApiResponse.ok(true);
    }
}


